#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio.h"
#include "hi_gpio.h"
#include "hi_io.h"
#include "hi_adc.h"
#include "hi_errno.h"
#define LED_TASK_STACK_SIZE 512
#define LED_TASK_PRIO 25
#define LED_TEST_GPIO 10 //10 for hispark_pegasus
#define TASK_DELAY_TIME 200000
#define F1 11 //5 for hispark_pegasus
#define F2 12

int LED_INTERVAL_TIME_US = 300000;

enum LedState {
    LED_ON = 0,
    LED_OFF,
    LED_SPARK,
};
enum LedSpeed {
    FAST = 0,
    MIDDLE,
    SLOW,
};
enum LedState g_LedState = LED_SPARK;
enum LedSpeed g_Ledspeed = FAST;

// F1按键中断处理
static void F1_Pressed(char *arg)
{
    (void)arg;
    enum LedState nextstate = LED_SPARK;
    printf(" F1 Pressed\n");
    switch (g_LedState) {
        case LED_ON:
            nextstate = LED_OFF;
            break;
        case LED_OFF:
            nextstate = LED_SPARK;
            break;
        case LED_SPARK:
            nextstate = LED_ON;
            break;
        default:
            break;
    }
    g_LedState = nextstate;
    usleep(TASK_DELAY_TIME);
}

// F2按键中断处理
static void F2_Pressed(char *arg)
{
    (void)arg;
    enum LedSpeed nextSpeed = MIDDLE;
    printf("F2 Pressed\n");
    switch (g_Ledspeed) {
        case FAST:
            nextSpeed = SLOW;
            break;
        case SLOW:
            nextSpeed = MIDDLE;
            break;
        case MIDDLE:
            nextSpeed = FAST;
            break;
        default:
            break;
    }
    g_Ledspeed = nextSpeed;
    usleep(TASK_DELAY_TIME);
}

static void ButtonledInit(void)
{
    IoTGpioInit(LED_TEST_GPIO);
    hi_io_set_func(LED_TEST_GPIO, HI_IO_FUNC_GPIO_10_GPIO);
    IoTGpioSetDir(LED_TEST_GPIO, IOT_GPIO_DIR_OUT);

    hi_gpio_init();
    hi_io_set_pull(F1, HI_IO_PULL_UP);
    hi_io_set_func(F1, HI_IO_FUNC_GPIO_12_GPIO);
    hi_gpio_set_dir(F1, HI_GPIO_DIR_IN);
    hi_gpio_register_isr_function(F1, 
                                HI_INT_TYPE_EDGE,
                                HI_GPIO_EDGE_FALL_LEVEL_LOW, 
                                &F1_Pressed, 
                                NULL);

    hi_io_set_pull(F2, HI_IO_PULL_UP);
    hi_io_set_func(F2, HI_IO_FUNC_GPIO_11_GPIO);
    hi_gpio_set_dir(F2, HI_GPIO_DIR_IN);
    hi_gpio_register_isr_function(F2, 
                                HI_INT_TYPE_EDGE, 
                                HI_GPIO_EDGE_FALL_LEVEL_LOW, 
                                &F2_Pressed, 
                                NULL);
}

// LED亮灭以及闪烁函数
static void *LedTask(const char *arg)
{
    (void)arg;
    while (1) {
        switch (g_LedState) {
            case LED_ON:
                IoTGpioSetOutputVal(LED_TEST_GPIO, 1);
                usleep(LED_INTERVAL_TIME_US);
                break;
            case LED_OFF:
                IoTGpioSetOutputVal(LED_TEST_GPIO, 0);
                usleep(LED_INTERVAL_TIME_US);
                break;
            case LED_SPARK:
                IoTGpioSetOutputVal(LED_TEST_GPIO, 0);
                usleep(LED_INTERVAL_TIME_US);
                IoTGpioSetOutputVal(LED_TEST_GPIO, 1);
                usleep(LED_INTERVAL_TIME_US);
                break;
            default:
                usleep(LED_INTERVAL_TIME_US);
                break;
        }
        switch (g_Ledspeed) {
            case FAST:
                LED_INTERVAL_TIME_US = 300000;
                break;
            case MIDDLE:
                LED_INTERVAL_TIME_US = 600000;
                break;
            case SLOW:
                LED_INTERVAL_TIME_US = 900000;
                break;
        }
    }
    return NULL;
}



// 线程处理函数
static void LedExampleEntry(void)
{
    osThreadAttr_t attr;

    ButtonledInit();

    attr.name = "LedTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = LED_TASK_STACK_SIZE;
    attr.priority = LED_TASK_PRIO;
    if (osThreadNew((osThreadFunc_t)LedTask, NULL, &attr) == NULL) {
        printf("[LedExample] Failed to create LedTask!\n");
    }
}

// 添加程序运行入口函数
SYS_RUN(LedExampleEntry);